<template>
  <basic-container v-loading="loading">
    <div class="el-page-header haruyuki-mb-20">
      <div
        class="el-page-header__left haruyuki-flex-center haruyuki-size-18"
        @click="goBackPage"
      >
        <i class="el-icon-arrow-left"></i>
        <div class="haruyuki-size-16">返回</div>
      </div>
      <div class="el-page-header__content">{{ $route.name }}</div>
    </div>
    <section class="merchant-container">
      <div class="merchant-padding" v-if="changeNo">
          <audit-record :rejectRecord="rejectRecord" ref="toAuditRecord" />
      </div>
      <alter-info :rpiNo="rpiNo" :rateType="rateType" :changeType="changeType" :aisleCode="aisleCode" :changeNo="changeNo" :isAlterDisabledShow="isAlterDisabledShow" @getDetailReject="getDetailReject" ref="toAlterForm" />
      <div class="haruyuki-flex-x-end haruyuki-mt-20 haruyuki-mb-40">
        <el-button size="medium" @click="goBackPage">返 回</el-button>
        <el-tooltip content="提交商户变更信息" placement="top">
            <el-button icon="el-icon-check" size="small" type="primary" :disabled="subDisabled" @click="handleConfirm">提 交</el-button>
        </el-tooltip>
      </div>
    </section>
  </basic-container>
</template>

<script>
import {
//   getList,
//   getDetail,
//   add,
//   update,
  addMerRecord,
  updateMerRecord,
//   remove,
} from "@/api/rb-service/merrecord/mer_change_record";
// import alterForm from "@/components/rb-merchant/alter-form.vue";
import alterInfo from "../component/alter_info.vue";
import auditRecord from "@/components/rb-merchant/audit-record.vue";
export default {
  components: {
    alterInfo,
    auditRecord,
  },
  data() {
    return {
      loading: false,
      rejectRecord: '',
      changeNo: '',
      rpiNo: '',
      changeType: '',
      aisleCode: '',
      rateType:'',
      subDisabled:false
    };
  },
  created() {
    if (this.$route.query.rpiNo) {
      this.rpiNo = this.$route.query.rpiNo;
      this.changeType = this.$route.query.changeType;
      this.aisleCode = this.$route.query.aisleCode;
      this.rateType = this.$route.query.rateType;
    } else if (this.$route.query.changeNo) {
      this.changeNo = this.$route.query.changeNo;
    }
    // if (this.$route.query.changeNo) {
    //   this.alterType = 'edit';
    //   this.changeNo = this.$route.query.changeNo;
    //   this.$refs['toAlterForm'].getMerhchantDetailPlus(this.changeNo);
    // } else {
    //   this.alterType = 'add';
    // }
  },
  methods: {
    /**
     * @func 商户信息确认
     * @desc 商户信息(变更/修改)确认
     * */
    handleConfirm() {
      let type = '';
      if (this.rpiNo) type = 'add';
      if (this.changeNo) type = 'edit';
      this.$refs["toAlterForm"].$refs["alterInfo"].validate((valid) => {
        if (valid) {
          if (this.$refs["toAlterForm"].alterForm.changeType === '0') {
            this.basicValidate(type);
          } else if (this.$refs["toAlterForm"].alterForm.changeType === '1') {
            this.cardValidate(type);
          } else if (this.$refs["toAlterForm"].alterForm.changeType === '2') {
            this.rateValidate(type);
          } else if (this.$refs["toAlterForm"].alterForm.changeType === '3') {
            this.accountValidate(type);
          } else if (this.$refs["toAlterForm"].alterForm.changeType === '4') {
            this.merSettleValidate(type);
          }
        } else {
          return false;
        }
      });
    },
    /**
     * @func 商户基本信息提交
     * @desc 商户基本信息提交
     * */
    basicValidate(type) {
      this.$refs["toAlterForm"].$refs["toBasicForm"].$refs["basicForm"].validate((valid) => {
        if (valid) {
          // if (this.$refs["toAlterForm"].alterForm.changeType == "1") {
          //   [
          //     this.$refs["toAlterForm"].alterForm.cardDTO.openAccProvinceCode,
          //     this.$refs["toAlterForm"].alterForm.cardDTO.openAccCityCode,
          //     this.$refs["toAlterForm"].alterForm.cardDTO.openAccAreaCode,
          //   ] = this.$refs["toAlterForm"].alterForm.cardDTO.accProvinceCityArea;
          // }
          [
            this.$refs["toAlterForm"].alterForm.basicDTO.mercProvCode,
            this.$refs["toAlterForm"].alterForm.basicDTO.mercCityCode,
            this.$refs["toAlterForm"].alterForm.basicDTO.mercAreaCode,
          ] = this.$refs["toAlterForm"].alterForm.basicDTO.mercProvinceCityArea;
          this.subDisabled = true;
          if (type == 'add') {
            addMerRecord(this.$refs["toAlterForm"].alterForm).then(
              (res) => {
                if (res.data.code === 200) {
                  this.$message({
                    type: "success",
                    message: "变更信息已经提交!",
                  });
                  this.subDisabled = false;
                  this.goBackPage();
                  
                }
              },
              (error) => {
                window.console.log(error);
                this.subDisabled = false;
              }
            );
          } else if (type == 'edit') {
            updateMerRecord(this.$refs["toAlterForm"].alterForm).then(
              (res) => {
                if (res.data.code === 200) {
                  this.$message({
                    type: "success",
                    message: "变更信息已经提交!",
                  });
                  this.subDisabled = false;
                  this.goBackPage();
                }
              },
              (error) => {
                this.subDisabled = false;
                window.console.log(error);
              }
            );
          }
        } else {
          return false;
        }
      });
    },
    /**
     * @func 商户结算信息提交
     * @desc 商户结算信息提交
     * */
    cardValidate(type) {
      this.$refs["toAlterForm"].$refs["toCardForm"].$refs["toCardForm"].$refs["cardForm"].validate((valid) => {
        if (valid) {
          [
            this.$refs["toAlterForm"].alterForm.cardDTO.openAccProvinceCode,
            this.$refs["toAlterForm"].alterForm.cardDTO.openAccCityCode,
            this.$refs["toAlterForm"].alterForm.cardDTO.openAccAreaCode,
          ] = this.$refs["toAlterForm"].alterForm.cardDTO.accProvinceCityArea;
          this.subDisabled = true;

          if (type == 'add') {
            addMerRecord(this.$refs["toAlterForm"].alterForm).then(
              (res) => {
                if (res.data.code === 200) {
                  this.$message({
                    type: "success",
                    message: "变更信息已经提交!",
                  });
                  this.goBackPage();
                  this.subDisabled = false;
                }
              },
              (error) => {
                window.console.log(error);
                this.subDisabled = false;
              }
            );
          } else if (type == 'edit') {
            updateMerRecord(this.$refs["toAlterForm"].alterForm).then(
              (res) => {
                if (res.data.code === 200) {
                  this.$message({
                    type: "success",
                    message: "变更信息已经提交!",
                  });
                  this.goBackPage();
                  this.subDisabled = false;
                }
              },
              (error) => {
                window.console.log(error);
                this.subDisabled = false;
              }
            );
          }
        } else {
          return false;
        }
      });
    },
    /**
     * @func 商户费率信息提交
     * @desc 商户费率信息提交
     * */
    rateValidate(type) {
      this.$refs["toAlterForm"].$refs["toRateForm"].$refs["rateForm"].validate((valid) => {
        if (valid) {
          let param = this.$refs["toAlterForm"].alterForm;
          this.rateType ? param.rateType = this.rateType : ''
          this.subDisabled = true;
          if (type == 'add') {
            addMerRecord(param).then(
              (res) => {
                if (res.data.code === 200) {
                  this.$message({
                    type: "success",
                    message: "变更信息已经提交!",
                  });
                  this.goBackPage();
                  this.subDisabled = false;
                }
              },
              (error) => {
                window.console.log(error);
                this.subDisabled = false;
              }
            );
          } else if (type == 'edit') {
            updateMerRecord(param).then(
              (res) => {
                if (res.data.code === 200) {
                  this.$message({
                    type: "success",
                    message: "变更信息已经提交!",
                  });
                  this.goBackPage();
                  this.subDisabled = false;
                }
              },
              (error) => {
                window.console.log(error);
                this.subDisabled = false;
              }
            );
          }
        } else {
          return false;
        }
      });
    },
    /**
     * @func 商户账号信息提交
     * @desc 商户账号信息提交
     * */
    accountValidate(type) {
      this.$refs["toAlterForm"].$refs["alterInfo"].validate((valid) => {
        if (valid) {
          this.subDisabled = true;
          if (type == 'add') {
            addMerRecord(this.$refs["toAlterForm"].alterForm).then(
              (res) => {
                if (res.data.code === 200) {
                  this.$message({
                    type: "success",
                    message: "变更信息已经提交!",
                  });
                  this.goBackPage();
                  this.subDisabled = false;
                }
              },
              (error) => {
                window.console.log(error);
                this.subDisabled = false;
              }
            );
          } else if (type == 'edit') {
            updateMerRecord(this.$refs["toAlterForm"].alterForm).then(
              (res) => {
                if (res.data.code === 200) {
                  this.$message({
                    type: "success",
                    message: "变更信息已经提交!",
                  });
                  this.goBackPage();
                  this.subDisabled = false;
                }
              },
              (error) => {
                window.console.log(error);
                this.subDisabled = false;
              }
            );
          }
        } else {
          return false;
        }
      });
    },
    /**
     * @func 商户清算周期变更提交
     * @desc 商户清算周期变更提交
     * */
     merSettleValidate(type) {
      this.$refs["toAlterForm"].$refs["alterInfo"].validate((valid) => {
        if (valid) {
          this.subDisabled = true;
          if (type == 'add') {           
            addMerRecord(this.$refs["toAlterForm"].alterForm).then(
              (res) => {
                if (res.data.code === 200) {
                  this.$message({
                    type: "success",
                    message: "变更信息已经提交!",
                  });
                  this.goBackPage();
                  this.subDisabled = false;
                }
              },
              (error) => {
                window.console.log(error);
                this.subDisabled = false;
              }
            );
          } else if (type == 'edit') {
            updateMerRecord(this.$refs["toAlterForm"].alterForm).then(
              (res) => {
                if (res.data.code === 200) {
                  this.$message({
                    type: "success",
                    message: "变更信息已经提交!",
                  });
                  this.goBackPage();
                  this.subDisabled = false;
                }
              },
              (error) => {
                window.console.log(error);
                this.subDisabled = false;
              }
            );
          }
        } else {
          return false;
        }
      });
    },
    /**
     * @func dialog商户信息取消
     * @desc dialog商户信息(变更/修改)取消
     * */
    handleCancel() {
      //   this.dialogVisible = false;
      this.emptyForm();
    },
    /**
     * @func 清空
     * @desc OEM表单信息 清空
     * */
    emptyForm() {
      this.$refs["toAlterForm"].$refs["alterInfo"].resetFields();
      this.$refs["toAlterForm"].basicForm = {};
      this.$refs["toAlterForm"].accountFrontList = [];
      this.$refs["toAlterForm"].accountReverseList = [];
      this.$refs["toAlterForm"].openPermitList = [];
      this.$refs["toAlterForm"].ecardFrontList = [];
      this.$refs["toAlterForm"].ecardReverseList = [];
    },
    /* 编辑时 dialog回显驳回的记录 */
    getDetailReject(data) {
      // this.rejectList = list;
      this.rejectRecord = data;
    },
    /* 返回页面 */
    goBackPage() {
      this.$router.$avueRouter.closeTag();
      this.$router.back();
    },
  },
};
</script>

<style>
</style>